<?php 
class App_model_router_position extends Model {
    private $table = 'router_position';
    
    public function add($router_id, $positon_id) {
        if ($this->exists($router_id, $position_id)) {
            return false;
        }
        
        return $this->db->insert($this->table, array(
            'router_id'     => $router_id,
            'position_id'   => $position_id
        ));
    }
    
    public function delete($id) {
        $id = (int) $id;
        
        if ($id <= 0) {
            tos_die();
        }
        
        return $this->db->delete($this->table, array('id'=>$id));
    }
    
    public function exists($router_id, $position_id) {
        $router_id = (int) $router_id;
        $position_id = (int) $position_id;
        
        if ($router_id <= 0 || $position_id <= 0) {
            $this->db->error();
        }
        
        $sql = "SELECT `id` FROM `{$this->table}` WHERE `router_id`='{$router_id}' AND `postion_id`='{$position_id}' LIMIT 1";
        if ($row = $this->db->row($sql)) {
            return true;
        } else {
            return false;
        }
    }
    
    public function contains($router_id, $position_ids) {
        $router_id = (int) $router_id;
        
        if ($router_id <= 0) {
            $this->db->error();
        }
        $position_ids = $this->db->escape($position_ids);
        $position_ids_str = "'" . implode("','", $position_ids) . "'";
        $sql = "SELECT `id` FROM `{$this->table}` WHERE `router_id`='{$router_id}' AND `postion_id` in ({$position_ids_str}) LIMIT 1";
        if ($row = $this->db->row($sql)) {
            return true;
        } else {
            return false;
        }
    }
}
?>